package com.liuguang.consumer.controller;

import com.liuguang.common.pojo.User;
import com.liuguang.common.util.UtilTest;
import com.liuguang.consumer.service.feign.ProviderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName: TestController
 * @Description：
 * @Author: wanghr
 * Date: 7:25 PM 2020/8/4
 * Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/consumer")
public class TestController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProviderService providerService;

    /**
     * 基本测试
     */
    @RequestMapping("/test1")
    public void test1() {
        log.info("准备调用生产者接口");
        log.info("消费者调用公共的工具类：{}", UtilTest.test1());
        String url = "http://localhost:8082/provider/test1";
        String forObject = restTemplate.getForObject(url, String.class);
        log.info("返回结果：{}", forObject);
    }

    /**
     * 测试使用feign 进行远程调用
     *
     * @params:
     * @return:
     */
    @RequestMapping("/test2")
    public void test2() {
//        String result = providerService.test1();

//        String s = "2222";
//        String result = providerService.test2(s);

        User user = new User("sss", "11");
        User user1 = providerService.test3(user);

        log.info("使用openFeign远程调用的结果:{}", user1);
    }

    /**
     * 测试服务降级
     *
     * @params: []
     * @return: void
     */
    @HystrixCommand(fallbackMethod = "back")
    @RequestMapping("/test3")
    public String test3() {
        String s = providerService.test4();
        log.info("调用结果:{}", s);
//        int a = 1/0;
        return s;
    }

    public String back() {
        return "服务不可用，服务降级";
    }
}
